package edu.infomatik.tundra.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.util.EnumSelectModel;

import edu.infomatik.tundra.data.DAOtundra;
import edu.infomatik.tundra.data.IBean;
import edu.infomatik.tundra.logic.UStoryBean;
import edu.infomatik.tundra.logic.UStoryTypes;
import edu.infomatik.tundra.logic.WorkBean;
import edu.infomatik.tundra.logic.SprintBean;
import edu.infomatik.tundra.logic.WorkKinds;
import edu.infomatik.tundra.logic.security.SystemAccessBean;
import edu.infomatik.tundra.model.ProjectIdEncoder;
import edu.infomatik.tundra.model.ProjectIdSelectModel;
import edu.infomatik.tundra.model.SprintIdEncoder;
import edu.infomatik.tundra.model.SprintIdSelectModel;
import edu.infomatik.tundra.model.StoryIdEncoder;
import edu.infomatik.tundra.model.StoryIdSelectModel;
import edu.infomatik.tundra.data.DataUtils;
@SuppressWarnings("unused")
public class CreateUStory {
	@SessionState
	private UStoryBean userStory;
	private boolean userStoryExists;
	private boolean newStory=false;
	@SessionState
	private SystemAccessBean user;
	private boolean userExists;
	@Inject
	private Messages messages;
	@SessionState
	private Boolean newWork;
	@Property
	private String usTypeLabel;
	@Property
	private String projectIdLabel;
	@Property
	private String sprintIdLabel;
	@Property
	private String storyIdLabel;
	@Property
	private String addRawP;
	@Property
	private String addRawSp;
	@Property
	private String addRawSt;
	
	private WorkBean work;
	private UStoryBean story;
	private SprintBean sprint;
	private UStoryTypes types;

	public String getSelectKind(){
		return messages.get("WorkKinds."+WorkKinds.get(userStory.getKind()).getName());
	}
	public SelectModel getusKind() {
		return new EnumSelectModel(WorkKinds.class, messages);
	}
	public SelectModel getusType() {
		return new EnumSelectModel(UStoryTypes.class, messages);
	}
	public UStoryTypes getUsTypes(){
		return types;
	}
	public void setUsTypes(UStoryTypes selectedType){
		types=selectedType;
		userStory.setType(selectedType);
	}
	
	public UStoryBean getUserStory() {
		if (!userStoryExists) {
			userStory=new UStoryBean();
			newStory = true;
		}
		return userStory;
	}
	
	public boolean isNewWork(){
		return newWork;
	}

	public void setUserStory(UStoryBean userStory) {
		this.userStory = userStory;
	}
	
	String onActivate(){
		addRawP="";
		addRawSp="";
		addRawSt="";
		storyIdLabel="<br/>.";
		if (!userExists) return "Index";
		if (!userStoryExists) userStory=new UStoryBean();
		if (null==userStory || userStory.getId()==0 || newWork.booleanValue() ){
			userStory.setId(DAOtundra.getInstance().getMaxId()+1);
			userStory.setDateCreated(DataUtils.getNow());
		}else{
			usTypeLabel = "(<i>"+messages.get("UStoryTypes."+UStoryTypes.get(userStory.getType()).getName())+"</i>)";
			WorkBean b = new WorkBean();
			// adding text to projectId-Label 
			if (userStory.getProjectId()>0){
				b.setId(userStory.getProjectId());
				projectIdLabel="<br/>(<i>"+((WorkBean)DAOtundra.getInstance().getBeanById(b)).getName()+"</i>)";
				addRawP="<br/>";
			} else projectIdLabel="";
			// adding text to sprintId-Label 
			if (userStory.getSprintId()>0){
				b.setId(userStory.getSprintId());
				sprintIdLabel="<br/>(<i>"+((WorkBean)DAOtundra.getInstance().getBeanById(b)).getName()+"</i>)";	
				addRawSp="<br/>";
			} else sprintIdLabel="";
			// adding text to storyId-Label 
			if (userStory.getLinkedWith()>0){
				b.setId(userStory.getLinkedWith());
				storyIdLabel="<br/>(<i>"+((WorkBean)DAOtundra.getInstance().getBeanById(b)).getName()+"</i>)";	
			}
		}
		newWork=new Boolean(false);
		return null;
	}
		
	public long getUserStoryId(){
		return userStory.getId();
	}
	
	void onSubmitFromUserStory(){
		DAOtundra.getInstance().update(userStory);
	}
	
	// Begin: methods for selectProject
	public List<WorkBean> getAllProjects(){
		List<WorkBean> wbList= new ArrayList<WorkBean>();
		for (IBean wb: DAOtundra.getInstance().getAllBeans(new WorkBean())){
			if (((WorkBean)wb).getKind()==WorkKinds.PROJECT_VALUE){
				wbList.add((WorkBean)wb);
			}
		}
		return wbList;
	}
	
	public SelectModel getProjectIdModel(){
		return new ProjectIdSelectModel(getAllProjects());
	}
	public ValueEncoder<WorkBean> getProjectIdEncoder(){
		return new ProjectIdEncoder();
	}
	
	public WorkBean getProjectIdSelection(){
		return work;
	}
	public void setProjectIdSelection(WorkBean selectedProject){
		work = selectedProject;
		userStory.setProjectId(selectedProject.getId());
	}
	// END: methods for selectProject
	
	// Begin: methods for selectUStory
	public List<UStoryBean> getAllStories(){
		List<UStoryBean> storyList= new ArrayList<UStoryBean>();
		for (IBean st: DAOtundra.getInstance().getAllBeans(new UStoryBean())){
			if (((UStoryBean)st).getKind()==WorkKinds.USTORY_VALUE){
				storyList.add((UStoryBean)st);
			}
		}
		return storyList;
	}
	
	public SelectModel getStoryIdModel(){
		return new StoryIdSelectModel(getAllStories());
	}
	
	public ValueEncoder<UStoryBean> getStoryIdEncoder(){
		return new StoryIdEncoder();
	}
	
	public UStoryBean getStoryIdSelection(){
		return story;
	}
	
	public void setStoryIdSelection(UStoryBean selectedStory){
		story = selectedStory;
		if (null!=selectedStory)
			userStory.setLinkedWith(selectedStory.getId());
		else userStory.setLinkedWith(-1);
	}
	// END: methods for selectUStory

	// Begin: methods for selectSprint
	public List<SprintBean> getAllSprints(){
		List<SprintBean> storyList= new ArrayList<SprintBean>();
		for (IBean sb: DAOtundra.getInstance().getAllBeans(new SprintBean())){
			if (((SprintBean)sb).getKind()==WorkKinds.SPRINT_VALUE){
				storyList.add((SprintBean)sb);
			}
		}
		return storyList;
	}
	
	public SelectModel getSprintIdModel(){
		return new SprintIdSelectModel(getAllSprints());
	}
	
	public ValueEncoder<SprintBean> getSprintIdEncoder(){
		return new SprintIdEncoder();
	}
	
	public SprintBean getSprintIdSelection(){
		return sprint;
	}
	
	public void setSprintIdSelection(SprintBean selectedSprint){
		sprint = selectedSprint;
		if (null!=selectedSprint)
			userStory.setSprintId(selectedSprint.getId());
		else userStory.setSprintId(-1);
	}
	// END: methods for selectSprint

}
